package com.longge;

public class IsLongPressedName {

    public boolean isLongPressedName(String name, String typed) {

        char[] nameChars = name.toCharArray();
        char[] typedChars = typed.toCharArray();
        int nameCount = 1, typedCount = 1, i = 0, j = 0;
        while (j < typedChars.length && i < nameChars.length) {
            char nameChar = nameChars[i];
            char typedChar = typedChars[j];
            if (nameChar!=typedChar) return false;
            while (i + 1 < nameChars.length && nameChar == nameChars[i + 1]) {
                nameCount++;
                i++;
            }

            while (j + 1 < typedChars.length && typedChar == typedChars[j + 1]) {
                typedCount++;
                j++;
            }
            if (typedCount < nameCount) return false;
            i++;
            j++;
            nameCount = 1;
            typedCount = 1;

        }
        while (j < typedChars.length){
            if (typedChars[j]!=nameChars[nameChars.length-1]){
                return false;
            }
            j++;
        }

        return i == nameChars.length;
    }

    public static void main(String[] args) {
        boolean longPressedName = new IsLongPressedName().isLongPressedName("saeed", "ssaaedd");
        System.out.println(longPressedName);
    }
}
